package yorkcc;

import java.awt.event.KeyEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JOptionPane;

public class DTDialog extends javax.swing.JDialog {

    public DTDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        SimpleDateFormat sdfd = new SimpleDateFormat("dd.MM.yyyy");
        jFormattedTextField1.setText(sdfd.format(Calendar.getInstance().getTime()));
        SimpleDateFormat sdft = new SimpleDateFormat("HH:mm");
        jFormattedTextField2.setText(sdft.format(Calendar.getInstance().getTime()));
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jFormattedTextField1 = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        jFormattedTextField2 = new javax.swing.JFormattedTextField();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Дата и время");
        setModal(true);
        setResizable(false);

        jButton1.setText("Отмена");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Текущая дата:");

        try {
            jFormattedTextField1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.##.####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFormattedTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jFormattedTextField1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jFormattedTextField1MouseClicked(evt);
            }
        });
        jFormattedTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jFormattedTextField1FocusLost(evt);
            }
        });
        jFormattedTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jFormattedTextField1KeyPressed(evt);
            }
        });

        jLabel2.setText("Текущее время:");

        try {
            jFormattedTextField2.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFormattedTextField2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jFormattedTextField2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jFormattedTextField2MouseClicked(evt);
            }
        });
        jFormattedTextField2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jFormattedTextField2FocusLost(evt);
            }
        });
        jFormattedTextField2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jFormattedTextField2KeyPressed(evt);
            }
        });

        jButton2.setText("Ok");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jFormattedTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(130, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jFormattedTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jFormattedTextField1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jFormattedTextField1MouseClicked
        if (evt.getClickCount() > 1) {
            SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
            jFormattedTextField1.setText(sdf.format(Calendar.getInstance().getTime()));
        }
}//GEN-LAST:event_jFormattedTextField1MouseClicked

    private void jFormattedTextField1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jFormattedTextField1FocusLost
        if (!ValidateData(jFormattedTextField1.getText())) {
            JOptionPane.showMessageDialog(null, "Ошибка в поле дата.", "Внимание!", JOptionPane.WARNING_MESSAGE);
            SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
            jFormattedTextField1.setText(sdf.format(Calendar.getInstance().getTime()));
        }
}//GEN-LAST:event_jFormattedTextField1FocusLost

    private void jFormattedTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jFormattedTextField1KeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_SPACE) {
            SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
            jFormattedTextField1.setText(sdf.format(Calendar.getInstance().getTime()));
            jFormattedTextField2.requestFocus();
        }
}//GEN-LAST:event_jFormattedTextField1KeyPressed

    private void jFormattedTextField2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jFormattedTextField2MouseClicked
        if (evt.getClickCount() > 1) {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            jFormattedTextField2.setText(sdf.format(Calendar.getInstance().getTime()));
        }
}//GEN-LAST:event_jFormattedTextField2MouseClicked

    private void jFormattedTextField2FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jFormattedTextField2FocusLost
        if (!ValidateTime(jFormattedTextField2.getText())) {
            JOptionPane.showMessageDialog(null, "Ошибка в поле время.", "Внимание!", JOptionPane.WARNING_MESSAGE);
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            jFormattedTextField2.setText(sdf.format(Calendar.getInstance().getTime()));
        }
}//GEN-LAST:event_jFormattedTextField2FocusLost

    private void jFormattedTextField2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jFormattedTextField2KeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_SPACE) {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            jFormattedTextField2.setText(sdf.format(Calendar.getInstance().getTime()));
            jButton2.requestFocus();
        }
}//GEN-LAST:event_jFormattedTextField2KeyPressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        String ds = "0" + jFormattedTextField1.getText().substring(0, 2) + jFormattedTextField1.getText().substring(3, 5);
        String ts = jFormattedTextField2.getText().substring(0, 2) + jFormattedTextField2.getText().substring(3, 5);

        Serial.sendCmd("5");
        String[] ps = Serial.getResp(0);
        Serial.sendCmd("p01");
        ps = Serial.getResp(0);
        Serial.sendCmd("e" + ds + "e" + ts + "e");
        ps = Serial.getResp(0);
        Serial.sendCmd("b");
        ps = Serial.getResp(0);

        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private Boolean ValidateData(String dtstr) {
        try {
            int day = Integer.parseInt(dtstr.substring(0, 2));
            if (day > 31 || day == 0) {
                return false;
            }
            int month = Integer.parseInt(dtstr.substring(3, 5));
            month--;
            if (month > 11 || month == -1) {
                return false;
            }
            int year = Integer.parseInt(dtstr.substring(6, 10));
        } catch (NumberFormatException ex) {
            //Logger.getLogger(NewZDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    private Boolean ValidateTime(String timstr) {
        try {
            int hour = Integer.parseInt(timstr.substring(0, 2));
            if (hour > 23) {
                return false;
            }
            int min = Integer.parseInt(timstr.substring(3, 5));
            if (min > 59) {
                return false;
            }
        } catch (NumberFormatException ex) {
        }
        return true;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JFormattedTextField jFormattedTextField2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables
}
